<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<title>人事管理系统 ——后台管理</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="cache-control" content="no-cache"/>
	<meta http-equiv="expires" content="0"/>
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"/>
	<meta http-equiv="description" content="This is my page"/>
	<link href="../css/css.css" type="text/css" rel="stylesheet"/>
	<link href="../css/pager.css" type="text/css" rel="stylesheet"/>
	<script type="text/javascript" src="../js/jquery-1.11.0.js"></script>
	<script type="text/javascript" src="../js/jquery-migrate-1.2.1.js"></script>
	<link href="../js/ligerUI/skins/Aqua/css/ligerui-dialog.css" rel="stylesheet" type="text/css"/>
	<script src="../js/ligerUI/js/core/base.js" type="text/javascript"></script>
	<script src="../js/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script>
	<script src="../js/ligerUI/js/plugins/ligerDrag.js" type="text/javascript"></script>
	<script src="../js/ligerUI/js/plugins/ligerResizable.js" type="text/javascript"></script>
</head>
<body>
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr>
			<td height="10"></td>
		</tr>
		<tr>
			<td width="15" height="32"><img src="../images/main_locleft.gif" width="15" height="32"></td>
			<td class="main_locbg font2"><img src="../images/pointer.gif">&nbsp;&nbsp;&nbsp;当前位置：文档管理 &gt; 修改文档
			</td>
			<td width="15" height="32"><img src="../images/main_locright.gif" width="15" height="32"></td>
		</tr>
	</table>

	<table width="100%" height="90%" border="0" cellpadding="10" cellspacing="0" class="main_tabbor">
		<tr valign="top">
			<td>
				<!-- 隐藏表单，flag表示添加标记 -->
				<input type="hidden" name="flag" value="2">
				<table width="100%" border="0" cellpadding="0" cellspacing="10" class="main_tab">

					<tr>
						<td class="font3 fftd">
							文档标题：<input type="text" name="title" size="30" id="title"/></td>
					</tr>
					<tr>
						<td class="main_tdbor"></td>
					</tr>
					<tr>
						<td class="font3 fftd">文档描述：<br/>
							<textarea name="remark" cols="88" rows="11" id="fileDescription"></textarea>
						</td>
					</tr>
					<tr>
						<td class="font3 fftd">文档：<br/>
							<input type="file" name="file" id="file" size="30"/>
						</td>
					</tr>
					<tr>
						<td class="main_tdbor"></td>
					</tr>
					<tr>
						<td class="font3 fftd">
							<input type="button" id="btn" value="上传">
							<input type="reset" value="重置" onclick="reset()">
						</td>
					</tr>
					<tr>
						<td class="main_tdbor"></td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
	<script type="text/javascript">
		function checkResultAndGetData($result) {
			if ($result.success == false) {
				throw $result;
			}
			return $result.data;
		}

		function reset() {
			window.location.replace('/csi/document/document.html');
		}


		$(document).ready(function () {
			/** 表单提交的校验 */
			$("#btn").click(function () {
				var title = $("#title").val();

				if ($.trim(title).length <= 2) {
					alert("请输入两个字符以上的标题");
					return;
				}
				var formData = new FormData();
				var fileDescription = $("#title").val();
				var fileTitle = $("#title").val();
				var file = $('#file')[0].files[0];
				formData.append('id', documentId);
				formData.append('fileTitle', fileTitle);
				formData.append('fileDescription', fileDescription);
				formData.append('file', file);
				$.ajax(
					{
						url: '/csi/document/upload',
						type: 'POST',
						data: formData,
						contentType: false, //禁止设置请求类型
						processData: false, //禁止jquery对data数据的处理,默认会处理，禁止的原因是,FormData已经帮我们做了处理
						dataType: "json",
						error: function (XMLHttpRequest, textStatus, text) {
							$.ligerDialog.error(JSON.stringify(text), "出错了，请于管理员联系");
						},
						success: function (result) {
							console.log(result);
							let data;
							try {
								data = checkResultAndGetData(result);
							} catch (error) {
								console.log(error)
								// alert(JSON.stringify(error));
								$.ligerDialog.error(JSON.stringify(error), "出错了，请于管理员联系");
								return;
							}
							if (data) {
								$.ligerDialog.success("添加成功");
							} else {
								$.ligerDialog.warn("添加失败");
							}
						}
					}
				);
			})
		});


		function submit() {

		}

		let documentId = null;
		$(document).ready(function () {
			// 获取url上的site参数并且传入表单
			let searchParams = new URLSearchParams(window.location.search);
			console.log(searchParams);
			documentId = searchParams.get("documentId");
			$.ajax({
				url: "api/get/" + documentId, // url where to submit the request
				type: "GET", // type of action POST || GET
				success: function (result) {
					// console.log(result);
					let data;
					try {
						data = checkResultAndGetData(result);
					} catch (error) {
						console.log(error)
						alert(JSON.stringify(error));
						return;
					}

					console.log(data);
					if (data == null) {
						return;
					}
					$("#title").attr("value", data.fileTitle);
					$("#fileDescription").attr("value", data.fileDescription);
				}
			});
		});
	</script>
</body>
</html>